package isr.cloudgate.dashboard.servlet;

import isr.cloudgate.dashboard.properties.Messages;
import isr.google.gdata.GDataClient;
import isr.google.gdata.data.GoogleGroup;
import isr.google.gdata.data.GoogleUser;

import java.io.IOException;
import java.util.Collection;
import java.util.Locale;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MainServlet
 */
public class MainServlet extends HttpServlet {

  private static final Logger logger = Logger.getLogger(MainServlet.class
    .getName());
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void
      doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

    logger.info("main page request");

    Locale locale = ServletUtilities.checkLanguage(request);

    /* check login data */
    GDataClient client = null;
    try {
      client = ServletUtilities.getAppsClient(request);
      if (client == null) {

        logger.info("null apps client");
        request.setAttribute("userMessage",
          Messages.getString(locale, Messages.Key.LOGIN_REQUIRED));
        request.getRequestDispatcher(JspPage.main).forward(request, response);
        return;

      }

      String action = request.getParameter("action");
      logger.info("action: " + action);

      if ("deleteuser".equals(action)) {

        String userId = request.getParameter("user_id");
        logger.info("delete user: " + userId);

        if (ServletUtilities.getAdminUser(request).equals(userId)) {

          request.setAttribute("userMessage",
            Messages.getString(locale, Messages.Key.DELETE_ADMIN));

        } else {

          try {

            client.deleteUser(userId);

          } catch (Exception e) {

            logger.info(e.toString());
            request.setAttribute("userMessage", e.toString());

          }

        }

        action = "loadusers";

      } else if ("deletegroup".equals(action)) {

        String groupId = request.getParameter("group_id");
        logger.info("delete group: " + groupId);

        try {

          client.deleteGroup(groupId);

        } catch (Exception e) {

          logger.info(e.toString());
          request.setAttribute("userMessage", e.toString());

        }

        action = "loadgroups";

      }

      if ("loadusers".equals(action)) {

        try {

          logger.info("getting google users");

          Collection<GoogleUser> userList = client.getAllUsers();

          if (userList == null)
            request
              .setAttribute("userMessage", String.format(
                Messages.getString(locale, Messages.Key.USER_LIST_RESPONSE),
                "0"));

          else {

            logger.info("user list: " + userList.size());

            request.setAttribute("userList", userList);
            request.setAttribute("userMessage", String.format(
              Messages.getString(locale, Messages.Key.USER_LIST_RESPONSE),
              userList.size()));

          }
        } catch (Exception e) {

          logger.info("error getting user list: " + e);
          request.setAttribute("userMessage", e.toString());

        }

      } else if ("loadgroups".equals(action)) {

        try {
          Collection<GoogleGroup> groupList = client.getAllGroups();

          if (groupList == null) {
            request.setAttribute("userMessage",
              String.format(
                Messages.getString(locale, Messages.Key.GROUP_LIST_RESPONSE),
                "0"));

          } else {

            logger.info("group list: " + groupList.size());

            request.setAttribute("groupList", groupList);
            request.setAttribute("userMessage", String.format(
              Messages.getString(locale, Messages.Key.GROUP_LIST_RESPONSE),
              groupList.size()));

          }
        } catch (Exception e) {

          logger.info("error getting group list: " + e);
          request.setAttribute("userMessage", e.toString());

        }

      }

    } catch (Exception e) {

      logger.info(e.toString());
      request.setAttribute("userMessage", e.toString());

    }

    request.getRequestDispatcher(JspPage.main).forward(request, response);

  }

}
